package br.com.felix.easyclearcase.ui.dialogs;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.CellEditor;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import br.com.felix.easyclearcase.EasyClearCaseExplorerConfiguration;
import br.com.felix.util.swing.CustomizedCellEditor;

/**
 * Dialog used to change the EasyClearCaseExplorer configuration.
 * */
public class EasyClearCaseExplorerConfigurationDialog extends JDialog {

	private static final long serialVersionUID = 8243322099423967961L;
	
	/**
	 * Table that holds the configuration variables.
	 * */
	private JTable tblConfig;
	
	
	/**
	 * Constructor. Initializes the UI.
	 * Obtains the configuration values.
	 * */
	public EasyClearCaseExplorerConfigurationDialog(){
		setLayout(new GridBagLayout());
		setTitle("Preferences");
		setSize(new Dimension(610,300));
		
		EasyClearCaseExplorerConfiguration config = EasyClearCaseExplorerConfiguration.instance();
		Object data[][]={
			{"Default comment for Add to Source Control",config.getDefaultAddToSourceControlComment()},
			{"Add to Source Control asks for user comment", config.getAddToSourceControlAsksForUserComment()}
		};
		final JTable tblConfig = new JTable(data,new String[]{"configuration item","value"});
		this.tblConfig = tblConfig;
		
		tblConfig.addMouseListener(new MouseAdapter(){
			@Override
			public void mousePressed(MouseEvent e) {
				if(e.getClickCount() >= 2){
					CellEditor ce = tblConfig.getCellEditor();
					if(ce!=null){
						if(tblConfig.getSelectedColumn()==0){
							ce.stopCellEditing();
						}
					}
				}
			}
		});
		tblConfig.getColumnModel().getColumn(1).setCellEditor(new CustomizedCellEditor());
		JScrollPane jspTable = new JScrollPane(tblConfig);
		jspTable.setPreferredSize(new Dimension(600,200));

		JButton btnSave = new JButton("Save");
		btnSave.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseReleased(MouseEvent e) {
				save();
			}
		});
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseReleased(MouseEvent e) {
				dispose();
			}
		});
		JPanel panelButtons = new JPanel();
		panelButtons.add(btnSave);
		panelButtons.add(btnCancel);
		
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.WEST;
		gbc.gridx=0;
		gbc.gridy=0;
		add(jspTable,gbc);
		gbc.gridy++;
		add(panelButtons,gbc);
	}
	
	
	/**
	 * Save the values entered by the user.
	 * */
	protected void save() {
		EasyClearCaseExplorerConfiguration config = EasyClearCaseExplorerConfiguration.instance();
		config.setDefaultAddToSourceControlComment((String) tblConfig.getValueAt(0, 1));
		Boolean addToSourceControlAsksForUserComment = (Boolean) tblConfig.getValueAt(1,1);
		System.out.println(addToSourceControlAsksForUserComment);
		config.setAddToSourceControlAsksForUserComment(addToSourceControlAsksForUserComment);
		dispose();
	}



	public static void main(String args[]){
		EasyClearCaseExplorerConfigurationDialog dialog = new EasyClearCaseExplorerConfigurationDialog();
		dialog.setVisible(true);
		dialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
}
